import 'package:flutter/material.dart';

class DismissibleWidgetPage extends StatefulWidget {
  @override
  _DismissibleWidgetPageState createState() => _DismissibleWidgetPageState();
}

class _DismissibleWidgetPageState extends State<DismissibleWidgetPage> {
  List<String> _items;

  @override
  void initState() {
    _items = new List<String>.generate(20, (i) => "Item ${i + 1}");
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("dissmissible widget"),
      ),
      body: new ListView.builder(
        itemCount: _items.length,
        itemBuilder: (context, index) {
          final item = _items[index];

          return new Dismissible(
            direction: DismissDirection.endToStart,
            // Each Dismissible must contain a Key. Keys allow Flutter to
            // uniquely identify Widgets.
            key: new Key(item),
            // We also need to provide a function that will tell our app
            // what to do after an item has been swiped away.
            onDismissed: (direction) {
              var toDeleteItem = _items[index];
              _items.removeAt(index);
              setState(() {});
              Scaffold.of(context).showSnackBar(new SnackBar(
                duration: Duration(milliseconds: 2000),
                content: new Text("$item dismissed"),
                action: SnackBarAction(
                  textColor: Colors.redAccent,
                  onPressed: () {
                    _items.insert(index, toDeleteItem);
                    setState(() {});
                  },
                  label: "取消",
                ),
              ));
            },
            // Show a red background as the item is swiped away
//            background: new Container(color: Colors.red),
            child: new ListTile(title: new Text('$item')),
          );
        },
      ),
    );
  }
}
